<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------

namespace api\wxapp\controller;

use api\wxapp\controller\ApiBaseController;
use think\Db;
use api\wxapp\model\Collection;
use api\wxapp\model\User;
use api\wxapp\model\ShopGoods;
use api\wxapp\model\ScoreGoods;
class CollectionController extends ApiBaseController
{
    public function index()
    {
        $params = $this->getParam();
        $this->checkgoodsId($params);
        $this->checkuid($params);
        $where[] = ['uid','eq',$params['uid']];
        $where[] = ['goods_id','eq',$params['goods_id']];
        if(Collection::where($where)->find()){
            $this->error('已经收藏过此商品!!!',false);
        }
        $params['addtime'] = $this->returnDateTime();
        $res = Collection::insert($params);
        if($res){
            $this->success('添加成功');
        }else{
            $this->error('添加失败');
        }
    }
    //
    public function user()
    {
        $params = $this->getParam();
        $this->checkuid($params);
        $shop_id = User::where('id',$params['uid'])->value('shop_id');
        $list = Collection::alias('t1')
            ->join('goods t2','t2.id = t1.goods_id')
            ->field('t2.name,t2.img,t2.para1,t1.id,t1.goods_id,t2.price,t1.para')
            ->where('t1.uid',$params['uid'])
            ->select()->each(function($item, $key) use ($shop_id){
                //判断当前商品的价格 是平台价格 还是店铺价格
                $map[] = ['shop_id','eq',$shop_id];
                $map[] = ['goods_id','eq',$item['goods_id']];
                $find = ShopGoods::where($map)->find();
                if($find){
                    //判断当前商品是否是积分商城商品
                    $map[] = ['status','eq',1];//上架  商品
                    $info = ScoreGoods::where($map)->find();
                    if($info){
                        $item['price'] = $info['price'];
                        $item['score'] = $info['score'];
                    }else{
                        $item['price'] = $find['price'];
                    }
                    $item['lable'] = '自提';
                }else{
                    $item['lable'] = '直邮';
                }
                return $item;
            });
        $this->success('请求成功!',$list);
    }
    public function delete()
    {
        $params = $this->getParam();
        $this->checkid($params);
        $res = Collection::destroy($params['id']);
        if($res){
            $this->success('删除成功!',true);
        }else{
            $this->error('删除失败!',false);
        }
    }
}
